//
//  HKPeoplePickerDelegate.m
//  HKAddressBookManager
//
//  Created by 胡锦涛 on 2018/12/27.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKPeoplePickerDelegate.h"

@implementation HKPeoplePickerDelegate

#pragma mark - CNContactPickerDelegate

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
{
    [picker dismissViewControllerAnimated:YES completion:^{
        [self addToExistingContactsWithPhoneNum:self.phoneNum userIcon:self.userIcon.length ? self.userIcon : @"default" contact:contact controller:self.controller];
    }];
}

#pragma mark - CNContactViewControllerDelegate

- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(CNContact *)contact
{
    [viewController dismissViewControllerAnimated:YES completion:nil];
    //    if (self.handler) {
    //        self.handler(contact);
    //    }
}


#pragma mark - Private
/**
 将号码添加某人的通讯录 (iOS 9 以上)
 
 @param phoneNum 号码
 @param contact 联系人
 @param userIcon 联系人头像
 @param controller 控制器
 */
- (void)addToExistingContactsWithPhoneNum:(NSString *)phoneNum
                                 userIcon:(NSString *)userIcon
                                  contact:(CNContact *)contact
                               controller:(UIViewController *)controller
{
    CNMutableContact *mutableContact = [contact mutableCopy];
    UIImage *originalImage = [UIImage imageNamed:userIcon.length ? userIcon : @"default"];
    if (originalImage) {
        mutableContact.imageData = UIImageJPEGRepresentation(originalImage, 1);
    }
    if (phoneNum.length) {
        CNLabeledValue *phoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile
                                                                      value:[CNPhoneNumber phoneNumberWithStringValue:phoneNum]];
        
        if (mutableContact.phoneNumbers.count > 0)
        {
            NSMutableArray *phoneNumbers = [mutableContact.phoneNumbers mutableCopy];
            [phoneNumbers addObject:phoneNumber];
            mutableContact.phoneNumbers = phoneNumbers;
        }
        else
        {
            mutableContact.phoneNumbers = @[phoneNumber];
        }
    }
    CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:mutableContact];
    contactController.delegate = self;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:contactController];
    [controller presentViewController:nav animated:YES completion:nil];
}
- (void)setExistingContactsWithUserIcon:(NSString *)userIcon
                               userName:(NSString *)userName
                                contact:(CNContact *)contact
                            complection:(void (^)( CNContact *))completcion
{
    CNMutableContact *mutableContact = [contact mutableCopy];
    mutableContact.imageData = nil;
    if (!contact.familyName.length) {
        mutableContact.familyName = userName.length ? userName : @"";
    }
    if (userIcon.length) {
        NSURL *url = [NSURL URLWithString:userIcon];
        NSData *tempData = [NSData dataWithContentsOfURL:url];
        mutableContact.imageData = tempData;
    }else {
        UIImage *originalImage = [UIImage imageNamed:@"default"];
        if (originalImage) {
            mutableContact.imageData = UIImageJPEGRepresentation(originalImage, 1);
        }
    }
    // 创建联系人请求
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest updateContact:mutableContact];
    // 重新写入
    CNContactStore *store = [[CNContactStore alloc] init];
    [store executeSaveRequest:saveRequest error:nil];
    completcion(mutableContact);
}

@end
